<?php
	// Funções Úteis
	
	function strdtTostrdt($s, $format_in, $format_out) {

        if (!trim($s)) return '00/00/00';
        
        $a = strdtToArray($s, $format_in);

		for($i = 0; $i < strlen($format_out); $i++){
			$c = substr($format_out, $i, 1);
			switch($c){
				case 'd':
					$res .= sprintf("%02d", $a["dia"]);
					break;
				case 'm':
					$res .= sprintf("%02d", $a["mes"]);
					break;
				case 'Y':
					$res .= $a["ano"];
					break;
				case 'H':
					$res .= sprintf("%02d", $a["horas"]);
					break;
				case 'i':
					$res .= sprintf("%02d", $a["minutos"]);
					break;
				case 's':
					$res .=  sprintf("%02d",$a["segundos"]);
					break;
				default:
					$res .= $c;
			}
		}
		return $res;
    }
	
	function strdtToArray($s, $format) {
        $dia = 1;
        $mes = 1;
        $ano = 2000;

        $horas = 0;
        $minutos = 0;
        $segundos = 0;

        for ($strpos = 0; $strpos < strlen($format); $strpos++) {
            $char = substr($format, $strpos, 1);

            switch($char) {
                case "d":
                    $dia = (int) substr($s, 0, 2);
                    $s = substr($s, 2);
                    break;
                case "m":
                    $mes = (int) substr($s, 0, 2);
                    $s = substr($s, 2);
                    break;
                case "Y":
                    $ano = (int) substr($s, 0, 4);
                    $s = substr($s, 4);
                    break;
                case "H":
                    $horas = (int) substr($s, 0, 2);
                    $s = substr($s, 2);
                    break;
                case "i":
                    $minutos = (int) substr($s, 0, 2);
                    $s = substr($s, 2);
                    break;
                case "s":
                    $segundos = (int) substr($s, 0, 2);
                    $s = substr($s, 2);
                    break;
                default:
                    $s = substr($s, 1);
            }
        }
        return array('dia' => $dia, 'mes' => $mes, 'ano' => $ano, 'horas' => $horas, 'minutos' => $minutos, 'segundos' => $segundos);
    }
    
    function dateSum($h) {
		foreach($h as $hr) {			
			list($g,$i,$s ) = explode(':', $hr);
			$seconds += $g * 3600;
			$seconds += $i * 60;
			$seconds += $s;
		}	
		
		$hours = floor( $seconds / 3600 );
		$seconds -= $hours * 3600;
		$minutes = floor( $seconds / 60 );
		$seconds -= $minutes * 60;
		
		$str = sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds);		
        return $str;
    }	
?>